package com.onpositive.datamap.values.impl;



public class LinkParser {

	
	public static void parseHyperlink(String hyperlink,ITokenAcceptor acceptor,String id) {
		int p = 0;
		int po = 0;

		while (p != -1) {
			p = hyperlink.indexOf("[[", p);

			if (p != -1) {
				acceptor.acceptPlainText(hyperlink.substring(po,p), 0);
				int indexOf = hyperlink.indexOf("]]", p);
				if (indexOf != -1) {
					String hContent = hyperlink.substring(p + 2, indexOf);
					int indexOf2 = hContent.indexOf('|');
					String title = hContent;
					String name = hContent;
					if (indexOf2 != -1) {
						name = hContent.substring(0, indexOf2);
						title = hContent.substring(indexOf2 + 1);
					}
					acceptor.acceptExternalIdentifier(title,name.trim(),id);
					po = indexOf + 2;
					p = po;
				} else {
					break;
				}
			}
		}
		acceptor.acceptPlainText(hyperlink.substring(po),0);
	}

	public static void parseHyperlink(String hyperlink, StringBuilder bld) {
		int p = 0;
		int po = 0;

		while (p != -1) {
			p = hyperlink.indexOf("[[", p);

			if (p != -1) {
				bld.append(hyperlink.substring(po, p));
				int indexOf = hyperlink.indexOf("]]", p);
				if (indexOf != -1) {
					String hContent = hyperlink.substring(p + 2, indexOf);
					int indexOf2 = hContent.indexOf('|');
					String title = hContent;
					if (indexOf2 != -1) {
						title = hContent.substring(indexOf2 + 1);
					}
					bld.append(title);
					po = indexOf + 2;
					p = po;
				} else {
					break;
				}
			}
		}
		if (po<hyperlink.length()){
			bld.append(hyperlink.substring(po));
		}
	}	
	
}